﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyTiemVang.DAO;
using QuanLyTiemVang.Classes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for PhieuChi.xaml
    /// </summary>
    /// 
    
    public partial class PhieuChi : Window, IPhanQuyenHelper
    {
        private class KhoanPhiView
        {
            public string TenKhoanPhi { get; set; }
            public decimal ThanhTien {get; set; }
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }
        public PhieuChi()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);
            dpNgayChi.SelectedDate = DateTime.Now.Date;
            //dgThongTinPhieuChi.ItemsSource = 
        }

        private void btnLapPhieu_Click(object sender, RoutedEventArgs e)
        {
            if (dpNgayChi.SelectedDate != null)
            {
                PHIEUCHI pc = PhieuChiDAO.getPhieuChi(dpNgayChi.SelectedDate.Value.Date);
                if (pc != null)
                {
                    List<CHITIETPHIEUCHI> listChiTietPhieuChi = PhieuChiDAO.getAllChiTietPhieuChi(pc);                    
                    List<KhoanPhiView> listKhoanPhi = new List<KhoanPhiView>();                
                    foreach (CHITIETPHIEUCHI temp in listChiTietPhieuChi)
                    {
                        KHOANPHI kp = ChiTietPhieuChiDAO.getKhoanPhi(temp);
                        KhoanPhiView khoanphi = new KhoanPhiView()
                        {
                            TenKhoanPhi = kp.TenKhoanPhi,
                            ThanhTien = decimal.Parse(temp.ThanhTien.ToString()),
                        };
                        listKhoanPhi.Add(khoanphi);
                    }
                    dgThongTinPhieuChi.ItemsSource = null;
                    dgThongTinPhieuChi.ItemsSource = listKhoanPhi.ToList();
                    txtMaPhieu.Text = pc.MaPhieuChi.ToString();
                    txtTongCong.Text = pc.TongCong.ToString();
                }
                else
                {
                    MessageBox.Show("Chưa tồn tại phiếu chi");
                }
            }
            else
            {
                MessageBox.Show("Không để trống ngày chi");
            }
            //String a = String.Format("{0:dd/MM/yyyy}", DateTime.Now);
            //MessageBox.Show(a);

        }

        void IPhanQuyenHelper.SetAdminFunction()
        {
            //throw new NotImplementedException();
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
            //throw new NotImplementedException();
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
            //throw new NotImplementedException();
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnTaoPhieuMoi_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("CHỨC NĂNG NÀY DÙNG ĐỂ LÀM GÌ ???");
        }

        private void btnXoaPhieu_Click(object sender, RoutedEventArgs e)
        {
            txtMaPhieu.Text = String.Empty;
            txtTongCong.Text = String.Empty;
            dgThongTinPhieuChi.ItemsSource = null;
            dgThongTinPhieuChi.Items.Refresh();
            dpNgayChi.SelectedDate = DateTime.Now.Date;

        }

        private void btnThemPhieuChi_Click(object sender, RoutedEventArgs e)
        {
            ChiTietPhieuChi chitietphieuchi = new ChiTietPhieuChi();
            chitietphieuchi.Show();
            //this.Hide();
        }
    }
}
